<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Partners extends CI_Controller {

	public $data = array();

	function __construct()
    {     	
        parent::__construct();
        $this->load->model("partner");
        $this->load->model("deal");
        $this->load->model("product");
        $this->load->model("coupon");
	}

	public function index(){	
		if(!$this->session->userdata("partner_id")) {
			$this->load->view('partner/login.php');
		} else {
			//Load necessary data
			$data["partner_name"] = $this->session->userdata("partner_name");
			$data["partner_email"] = $this->session->userdata("partner_email");

			$this->load->view('partner/home.php', $data);
		}
	}

	public function login() {
		if(isset($_POST["email"]) && !$this->session->userdata("partner_id")) {
			$form = $_POST;
			
			$email = strtolower($form["email"]);
			$pass = md5($form["password"]);
			$partners = $this->partner->partnerExist($email, $pass);

			if($partners) {
				$partner = $partners[0];
				$this->session->set_userdata("partner_id", $partner->id);
				$this->session->set_userdata("partner_name", $partner->name);
				$this->session->set_userdata("partner_email", $partner->email);

				header("Location: /partners/");
			} else {
				$data["error"] = "Неверная комбинация имя/пароль";
				$this->load->view('partner/login.php', $data);
			}
		} else if(!$this->session->userdata("partner_id")){
			$this->load->view('partner/login.php');
		} else {
			header("Location: /partners/");
		}
	}

	public function logout() {
		$this->session->sess_destroy();
		header("Location: /partners/");
	}

	public function about() {
		if(!$this->session->userdata("partner_id")) {
			$this->load->view('partner/login.php');
		} else {
			//Load necessary data
			$data["partner_name"] = $this->session->userdata("partner_name");
			$data["partner_email"] = $this->session->userdata("partner_email");

			$this->load->view('partner/about.php', $data);
		}
	}

	public function deals() {
		if(!$this->session->userdata("partner_id")) {
			$this->load->view('partner/login.php');
		} else {
			//Load necessary data
			$data["partner_name"] = $this->session->userdata("partner_name");
			$data["partner_email"] = $this->session->userdata("partner_email");

			$data["deals"] = $this->deal->getDealByPartnerId($this->session->userdata("partner_id"));

			$this->load->view('partner/deals.php', $data);
		}
	}

	public function products() {
		if(!$this->session->userdata("partner_id")) {
			$this->load->view('partner/login.php');
		} else {
			//Load necessary data
			$data["partner_name"] = $this->session->userdata("partner_name");
			$data["partner_email"] = $this->session->userdata("partner_email");

			for ($i = 0; $i < func_num_args(); $i+=2) {
				$data[func_get_arg($i)] = func_get_arg($i + 1);
			}
			if(isset($data["id"])) {
				$data["products"] = $this->product->getProductsByDealId($data["id"]);

				$this->load->view('partner/products.php', $data);
			} else {
				$data["products"] = $this->product->getProductsByPartnerId($this->session->userdata("partner_id"));
				$this->load->view('partner/products.php', $data);
			}

		}
	}

	public function coupons() {
		if(!$this->session->userdata("partner_id")) {
			$this->load->view('partner/login.php');
		} else {
			//Load necessary data
			$data["partner_name"] = $this->session->userdata("partner_name");
			$data["partner_email"] = $this->session->userdata("partner_email");

			for ($i = 0; $i < func_num_args(); $i+=2) {
				$data[func_get_arg($i)] = func_get_arg($i + 1);
			}

			if(isset($data["prodid"])) {
				$data["coupons"] = $this->coupon->getCouponByProductId($data["prodid"]);
			} else if(isset($data["dealid"])) {
				$data["coupons"] = $this->coupon->getCouponByProductId($data["dealid"]);
			} else {
				$data["coupons"] = $this->coupon->getCouponByPartnerId($this->session->userdata("partner_id"));
			}

			$this->load->view('partner/coupons.php', $data);
		}
	}

	public function find() {
		if(!$this->session->userdata("partner_id")) {
			$this->load->view('partner/login.php');
		} else {
			//Load necessary data
			$data["partner_name"] = $this->session->userdata("partner_name");
			$data["partner_email"] = $this->session->userdata("partner_email");

			$data["coupons"] = $this->coupon->find($_POST["code"], $this->session->userdata("partner_id"));
			
			$this->load->view('partner/coupons.php', $data);
		}
	}

	public function usecoupon()	 {
		$code = func_get_arg(1);

		$this->coupon->useCoupon($code, $this->session->userdata('partner_id'));

		header("Location: /partners/coupons");
	}
}